package com.kc.sjwh.action;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.po.Zhzxpz;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.service.ZhzxpzService;

/**
 * 指挥中心配置
 * @author zhouy
 *
 */
@Controller
@RequestMapping("zhzxpz")
public class ZhzxpzAction extends BaseAction{

	@Autowired
	ZhzxpzService zhzxpzService;
	
	/**
	 * 进入指挥中心配置列表页
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String listPage(HttpServletRequest req){
		return "zhzxpz/zhzxpzList";
	}
	
	/**
	 * 进入编辑页面
	 * @param req
	 * @param id
	 * @return
	 */
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id){
		req.setAttribute("act", id==null?"新增":"修改");
		Zhzxpz pz = new Zhzxpz();
		if(null != id){
			pz = zhzxpzService.getOneData(id);
		}
		req.setAttribute("obj", pz);
		return "zhzxpz/zhzxpzForm";
	}
	
	/**
	 * 获得列表数据
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getData")
	public String getData(HttpServletRequest req, int pageSize, int pageNum){
		QueryParam p = new QueryParam(pageSize, pageNum);
		String zhzxbh = req.getParameter("zhzxbh");
		if(null != zhzxbh && !"".equals(zhzxbh)){
			p.addSqlCondition(" t.zhzxbh = '" + zhzxbh + "'");
		}
		String pzxname = req.getParameter("pzxname");
		if(null != pzxname){
			pzxname = pzxname.trim();
			if(!"".equals(pzxname)){
				p.addSqlCondition(" gx.zbxmc like '%" + pzxname + "%'");
			}
		}
		p.setOrder(" t.gxsj desc,t.bh asc");
		Page<Zhzxpz> page = zhzxpzService.getData(p);
		/*Table table = new Table();
		table.add(new Tr("指挥中心", "指标项", "指标项值", "创建时间", "更新时间", "操作"));//表头
		for(Zhzxpz one : page.getList()){
			table.add(new Tr(
					new Td(one.getZhzxqmc()),
					new Td(one.getZbxmc()),
					new Td(one.getZbxz()),
					new Td(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(one.getCjsj())),
					new Td(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(one.getGxsj())),
					new Td("<a href='form?id="+one.getBh()+"'>修改</a> &nbsp; "+ 
							"<a onclick=\"deleteData('"+one.getBh()+"')\">删除</a>")
			));
		}
		Map data = new HashMap();
		data.put("total", page.getTotal());
		data.put("html", table.toString());*/
		return JSON.toJSONString(new AjaxRes(page));
	}
	
	/**
	 * 保存编辑
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(HttpServletRequest req){
		String type = req.getParameter("czType");
		//判断属性代码是否存在
		Zhzxpz one = new Zhzxpz();
		if("add".equals(type)){
			String bh = UUID.randomUUID().toString().replace("-", "");
			one.setBh(bh);
			one.setZbxbh(req.getParameter("zbxbh"));
			one.setZhzxbh(req.getParameter("zhzxbh"));
			one.setZbxz(req.getParameter("zbxz"));
			zhzxpzService.addData(one);
		}else{
			one.setBh(req.getParameter("bh"));
			one.setGxsj(new Date());
			one.setZbxz(req.getParameter("zbxz"));
			zhzxpzService.editData(one);
		}
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 删除数据
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String deleteData(HttpServletRequest req){
		Zhzxpz one = new Zhzxpz();
		one.setBh(req.getParameter("id"));
		one.setSybz("N");
		zhzxpzService.editData(one);
		return JSON.toJSONString(new AjaxRes());
	}
}
